﻿/*
* <form action="/Diary/QuickPost" 
data-tientq-ajax="true" 
data-tientq-ajax-method="post" 
data-tientq-ajax-mode="html/before/after/prepend/append/" 
data-tientq-ajax-update="#ajax-target" 
id="form0" method="post" novalidate="novalidate">
* event: onBeforeSerialize befor serialize form
*/
$(document).ready(function () {
    var ajaxFormSubmit = function () {
        // event befor serialize form
        $(this).trigger("onBeforeSerialize");
        // bind value before form submit
        var $form = $(this);
        var options = {
            url: $form.attr("action"),
            type: $form.attr("data-tientq-ajax-method"),
            data: $form.serialize()
        };
        $.ajax(options).done(function (data) {
            var mode = $form.attr("data-tientq-ajax-mode");
            if (mode != 'event') {
                var $target = $($form.attr("data-tientq-ajax-update"));
                switch (mode) {
                    case 'before':
                        $target.before(data);
                        break;
                    case 'after':
                        $target.after(data);
                        break;
                    case 'prepend':
                        $(data).prepend($target);
                        break;
                    case 'append':
                        $(data).insertAfter($target);
                        break;
                    default:
                        $target.html(data);
                }
            } else {

            }
            $form.find('input[type=text], input[type=password], textarea').val("");
            $form.trigger('onComplete', data);
        });
        return false;
    }
    
    $('form[data-tientq-ajax="true"]').submit(ajaxFormSubmit);

    // more less text
    var maxLines = 3;
    var moretext = "+more";
    var lesstext = "-less";
    $('.more').each(function () {
        var height = maxLines * $(this).css('line-height').replace('px', '');
        if ($(this).height() > height) {
            $(this).append('<a href="#" class="morelink">' + moretext + '</a>');
            $(this).find('.more-content').css('max-height', height).css('overflow', 'hidden');
        }
    });

    $(".morelink").click(function () {
        if ($(this).hasClass("less")) {
            $(this).removeClass("less");
            $(this).html(moretext);
            var $moreContent = $(this).prev('div');
            var height = maxLines * $moreContent.css('line-height').replace('px', '');
            $moreContent.css('max-height', height).css('overflow', 'hidden');
        } else {
            $(this).addClass("less");
            $(this).html(lesstext);
            $(this).prev('div').css('max-height', 'initial').css('overflow', 'visible');
        }
        return false;
    });
    // more less text
});